package zhihui.mobile.presenter.shop;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.bean.shop.cart.CartInfo;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.shop.ShopAttentionManager;
import zhihui.mobile.manager.shop.ShopCartManager;
import zhihui.mobile.manager.shop.ShopManager;
import zhihui.mobile.manager.shop.ShopOrderManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.cart.IShoppingCartContract;

/**
 * Created by 张公铭 on 2017/8/8 0008.
 */

public class ShoppingCartPresenter extends BaseShopPresenter implements IShoppingCartContract.IShoppingCartPresenter
{
    private Context mContext;
    private IShoppingCartContract.IShoppingCartView cartView;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        cartView = (IShoppingCartContract.IShoppingCartView) view;
    }

    @Override
    public void onResume()
    {
        initData();
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void initData()
    {
        cartView.updateCartNum(ShopManager.getInstance().getCartInfo().getCartNum());
        cartView.initList(ShopManager.getInstance().getCartInfo().getSkus());
    }

    @Override
    public void delete()
    {
        final CartInfo cartInfo = ShopManager.getInstance().getCartInfo();
        int sumNum = cartInfo.getCartNum();
        List<SkuInfo> list = cartInfo.getSkus();
        List<SkuInfo> deleteList = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        for (SkuInfo skuInfo : list)
        {
            if (skuInfo.isSelect())
            {
                if (!sb.toString().equals(""))
                {
                    sb.append(",");
                }
                sumNum -= skuInfo.getNum();
                sb.append(skuInfo.getSkuId());
                deleteList.add(skuInfo);
            }
        }
        if (deleteList.size() > 0)
        {
            cartInfo.setCartNum(sumNum);
            for (SkuInfo skuInfo : deleteList)
            {
                list.remove(skuInfo);
            }
            cartInfo.setSkus(list);
        }

        if (!sb.toString().equals("") && deleteList.size() > 0)
        {
            cartView.showLoading();
            List<Object> params = new ArrayList<>();
            params.add(UserManager.getInstance().getCurUser().getId());
            params.add(sb.toString());
            ShopManager.getInstance().deleteCart(new IHttpCallBackListener()
            {
                @Override
                public void success(JSONObject result)
                {
                    cartView.hindLoading();
                    ShopManager.getInstance().setCartInfo(cartInfo);
                    cartView.updateCartNum(ShopManager.getInstance().getCartInfo().getCartNum());
                    cartView.initList(ShopManager.getInstance().getCartInfo().getSkus());
                    cartView.showMessage("删除成功！");
                }

                @Override
                public void failed(String message)
                {
                    cartView.hindLoading();
                    cartView.showMessage("删除失败！");
                }
            }, params);
        }
    }

    @Override
    public void attention()
    {
        CartInfo cartInfo = ShopManager.getInstance().getCartInfo();
        List<SkuInfo> list = cartInfo.getSkus();
        StringBuffer sb = new StringBuffer();
        for (SkuInfo skuInfo : list)
        {
            if (skuInfo.isSelect())
            {
                if (!sb.toString().equals(""))
                {
                    sb.append(",");
                }
                sb.append(skuInfo.getSpuId());
            }
        }

        if (!sb.toString().equals(""))
        {
            cartView.showLoading();
            List<Object> params = new ArrayList<>();
            params.add(UserManager.getInstance().getCurUser().getId());
            params.add(sb.toString());

            ShopAttentionManager.getInstance().attention(new IHttpCallBackListener()
            {
                @Override
                public void success(JSONObject result)
                {
                    cartView.hindLoading();
                    cartView.showMessage("关注成功！");
                }

                @Override
                public void failed(String message)
                {
                    cartView.hindLoading();
                    cartView.showMessage("关注失败！");
                }
            }, params);
        }
    }

    @Override
    public void goAttention()
    {
        cartView.showLoading();
        ShopAttentionManager.getInstance().getMyAttention(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                cartView.hindLoading();
                Gson gson = new Gson();
                List<SpuInfo> list = null;
                try
                {
                    list = gson.fromJson(result.getJSONArray("data").toString(), new TypeToken<List<SpuInfo>>()
                    {
                    }.getType());
                    ShopAttentionManager.getInstance().setList(list);
                    cartView.goShopAttention();
                } catch (JSONException e)
                {
                    cartView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                cartView.hindLoading();
                cartView.showMessage(message);
            }
        });
    }

    //确认订单
    @Override
    public void comfirmOrder()
    {
        //判断是否勾选商品
        if (ShopManager.getInstance().getSelectSku() == null || ShopManager.getInstance().getSelectSku().size() <= 0)
        {
            cartView.showMessage("请选择需要购买的商品！");
            return;
        }
        ShopOrderManager.getInstance().setConfirmList(ShopManager.getInstance().getSelectSku());
        cartView.goComfirmOrderActivity();
    }

    @Override
    public void updateCart()
    {
        if (ShopManager.getInstance().getCartInfo().getSkus().size() == 0)
        {
            cartView.goBack(mContext.getClass());
            return;
        }

        cartView.showLoading();
        ShopManager.getInstance().updateCartNum(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                cartView.hindLoading();
                cartView.goBack(mContext.getClass());
            }

            @Override
            public void failed(String message)
            {
                cartView.hindLoading();
                cartView.showMessage(message);
            }
        });
    }
}
